package controller;

import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author rizqi
 */
public class Controller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, Exception {
        String page = request.getParameter("page");
        RequestDispatcher dis = null;
        if (page != null) {
            if (page.equals("listcerita")) {
                LogicCerita list = new LogicCerita(request);
                dis = request.getRequestDispatcher(list.listCerita());
            } else if (page.equals("form_addcerita")) {
                dis = request.getRequestDispatcher("form.jsp");
            } else if (page.equals("addcerita")) {
                LogicCerita add = new LogicCerita(request);
                dis = request.getRequestDispatcher(add.add());
            } else if (page.equals("deletecerita")) {
                LogicCerita delete = new LogicCerita(request);
                dis = request.getRequestDispatcher(delete.delete());
            } else if (page.equals("formupdateCerita")) {
                LogicCerita selectone = new LogicCerita(request);
                dis = request.getRequestDispatcher(selectone.selectone());
            } else if (page.equals("updatecerita")) {
                LogicCerita update = new LogicCerita(request);
                dis = request.getRequestDispatcher(update.update());
            }
        } else {
            dis = request.getRequestDispatcher("index1.jsp");
        }
        dis.forward(request, response);
    }     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
